package com.xz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.R;
import com.xz.VideoInfo;
import com.xz.entity.Member;
import com.xz.entity.Video;
import com.xz.entity.Vplayer;
import com.xz.entity.vo.MemberQuery;
import com.xz.entity.vo.VideoQuery;
import com.xz.entity.vo.VideoVo;
import com.xz.service.VideoService;
import com.xz.service.VplayerService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFPivotTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-09
 */
@RestController
@RequestMapping("/video/vod")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private VplayerService vplayerService;
    @PostMapping("pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current, @PathVariable long limit,
                           @RequestBody(required = false) VideoQuery videoQuery) {
        QueryWrapper<Video> queryWrapper=new QueryWrapper<>();
        //创建page对象
        Page<Video> page = new Page<>(current,limit);
        /*
         得到对象
         */
        String username = videoQuery.getUsername();
        String begin = videoQuery.getBegin();
        String end=videoQuery.getEnd();
        if(!StringUtils.isEmpty(username)) queryWrapper.like("username",username);
        if(!org.springframework.util.StringUtils.isEmpty(begin)) queryWrapper.ge("gmt_create",begin);
        if(!org.springframework.util.StringUtils.isEmpty(end)) queryWrapper.le("gmt_create",end);
        //排序
        queryWrapper.orderByDesc("gmt_create");
        IPage<Video> pageInfo = videoService.page(page, queryWrapper);
        long total = pageInfo.getTotal();
        List<Video> list = pageInfo.getRecords(); //数据list集合
        return R.ok().data("total",total).data("list",list);
    }
    /*
    增加一个视频
     */
    @PostMapping("addVideoInfo")
    public R addVideoInfo(@RequestBody Video video){
        String id = videoService.saveVideoInfo(video);
        return R.ok().data("Id",id);
    }

    @GetMapping("getVideoInfo/{Id}")
    public R getCourseInfo(@PathVariable("Id")String videoId){
        Video InfoVo=videoService.getById(videoId);
        return R.ok().data("InfoVo",InfoVo);
    }

    //修改信息
    @PostMapping("updateInfo")
    public R updateInfo(@RequestBody Video video){
        boolean flag = videoService.updateById(video);
        if (flag) return R.ok();
        else return R.error();
    }

    //判断是否存在一个视频
    @GetMapping("judge/{id}")
    public R getjudge(@PathVariable String id){
        QueryWrapper<Vplayer> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("video_id", id);
        Vplayer vplayer = vplayerService.getOne(queryWrapper);
        return R.ok().data("vplayer",vplayer);
    }

    //删除所有
    //removeAll
    @GetMapping("removeAll/{id}")
    public R removeAll(@PathVariable String id){
        boolean flag=videoService.removeAll(id);
        if (flag) return R.ok();
        else return R.error();
    }

    @GetMapping("getVideo/{id}")
    public VideoInfo getVideo(@PathVariable String id){
         Video video=videoService.getById(id);
         VideoInfo videoInfo=new VideoInfo();
         videoInfo.setVideoId(id);
        videoInfo.setTitle(video.getTitle());
        videoInfo.setCover(video.getCover());
        videoInfo.setPrice(video.getPrice());
        return videoInfo;
    }
}

